package com.zttek.photosShareProject.modules.web.manager;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.base.MyBaseController;
import com.zttek.photosShareProject.common.keyword.SensitivewordFilter;
import com.zttek.photosShareProject.modules.model.Keyword;
import com.zttek.photosShareProject.modules.service.manager.IKeywordService;
import com.zttek.photosShareProject.modules.service.manager.impl.KeywordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by 13296 on 2017/8/16.
 */
@Controller
public class KeywordController  extends MyBaseController {
    @Autowired
    private IKeywordService keywordService;

    @RequestMapping(value = "admin/keyword",method = RequestMethod.GET)
    public String keywordView(int pn,String addKeywordMessage,String deleteKeywordMessage,Model model){
        List<Keyword> list = null;
        Page<Keyword> page = new Page<>(pn,10);
        try {
            page = keywordService.selectKeywordPage(page);
            list = page.getRecords();
            System.out.println("total:"+page.getTotal());
            System.out.println("pages:"+page.getPages());
            model.addAttribute("pages",page.getPages());
            model.addAttribute("keywordList",list);
            model.addAttribute("pn",pn);

            model.addAttribute("addKeywordMessage",addKeywordMessage);
            model.addAttribute("deleteKeywordMessage",deleteKeywordMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "admin/keyword",method = RequestMethod.POST)
    public String keyword(Model model){
        return null;
    }

    @RequestMapping(value = "admin/searchKeyword",method = RequestMethod.GET)
    public String searchKeywordView(int pn ,String searchKeywordValue, Model model){
        List<Keyword> list = null;
        Page<Keyword> page = new Page<>(pn,10);
        System.out.println("searchKeywordValue:"+searchKeywordValue);
        try {
            page = keywordService.selectLike(page,searchKeywordValue);
            list = page.getRecords();
            System.out.println("total:"+page.getTotal());
            System.out.println("pages:"+page.getPages());
            model.addAttribute("pages",page.getPages());
            model.addAttribute("keywordList",list);
            model.addAttribute("pn",pn);
            model.addAttribute("searchKeywordValue",searchKeywordValue);
            if (searchKeywordValue.trim().length()==0&&searchKeywordValue.trim().equals("")){
                model.addAttribute("emptyValue",true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "admin/searchKeyword",method = RequestMethod.POST)
    public String searchKeyword(int pn ,String searchKeywordValue, Model model){
        searchKeywordView(pn,searchKeywordValue,model);
        return null;
    }

    @RequestMapping(value = "admin/deleteKeyword",method = RequestMethod.GET)
    @ResponseBody
    public String deleteKeywordView(String pn,Integer id,Model model){
        System.out.println("delete id:"+id);
        try {
            keywordService.deleteKeywordById(id);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("deleteKeywordMessage","删除失败！");
            return "error";
        }
        model.addAttribute("deleteKeywordMessage","删除成功！");
        return "success";
    }


    @RequestMapping(value = "admin/deleteKeyword",method = RequestMethod.POST)
    public String deleteKeyword(Model model){
        return null;
    }

    @RequestMapping(value = "admin/addKeyword",method = RequestMethod.GET)
    public String addKeywordView(Model model){
        return null;
    }

    @RequestMapping(value = "admin/addKeyword",method = RequestMethod.POST)
    public String addKeyword(String pn,String keyword,String kwType,Model model){
        Date date = new Date();
        Keyword kw = new Keyword();
        kw.setCreateTime(date);
        kw.setKeyword(keyword);
        kw.setKwType(kwType);
        try {
           // System.out.println("keyword:"+keyword);
            keywordService.insertKeyword(kw);
        } catch (Exception e) {
            model.addAttribute("addKeywordMessage","该关键字已存在！");
            //e.printStackTrace();
            return "redirect:keyword?pn="+pn;
        }
        model.addAttribute("addKeywordMessage","添加成功！");
        return "redirect:keyword?pn="+pn;
    }

    @RequestMapping(value = "admin/updateKeyword",method = RequestMethod.GET)
    public String updateKeywordView(Model model){
        return null;
    }

    @RequestMapping(value = "admin/updateKeyword",method = RequestMethod.POST)
    public String updateKeyword(Model model){
        return null;
    }

}
